package com.yh.blog.controller;

import com.yh.blog.domain.Blog;
import com.yh.blog.domain.Types;
import com.yh.blog.service.BlogService;
import com.yh.blog.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 *  分类前台展示控制层
 */
@Controller
public class TypesShowController {
    @Autowired
    private TypesService typesService;
    @Autowired
    private BlogService blogService;

//      跳转到分类页面
    @GetMapping("/types/{typesId}")
    public String index(@RequestParam(value = "page",defaultValue = "1") Integer page, @PathVariable("typesId") Integer typesId, Model model){
        List<Types> types=this.typesService.list();
//        从首页过来的传递其分类id为-1  这是为了初次显示显示第一个标签下拥有的博客
        if(typesId==-1){
            typesId=types.get(0).getTypesId();
        }
        model.addAttribute("types",types);
        Blog condition=new Blog();
        condition.setTypesId(typesId);
        condition.setPublish(true);
//        传递当前分类下所有的博客
        model.addAttribute("blogs",this.blogService.list(condition));
//        这里是为了让选中的分类有一个颜色上的区分 所以将当前id传回前台
        model.addAttribute("activeByTypesId",typesId);
        return "types";
    }
}
